package com.ty.wx.filter;

import com.ty.bean.Courier;
import com.ty.bean.User;
import com.ty.util.UserUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @WebFilter({"/index.html","/wx/*","*.do"})
 * @author 田野
 */
@WebFilter({"/index.html"})
public class UserFilter implements Filter {
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        String requestURI = request.getRequestURI();
        System.out.println("wxURI"+requestURI);

        User wxUser = UserUtil.getWxUser(request.getSession());
        Courier wxCourier = UserUtil.getWxCourier(request.getSession());
        if (wxUser != null || wxCourier != null){
            // 这个是放行的意思
            chain.doFilter(req, resp);
        }else {
            response.sendRedirect("/login.html");
        }


    }

    @Override
    public void init(FilterConfig config) throws ServletException {
        System.out.println("微信端鉴权过滤器启动");
    }

}
